package github.fafaovo.common.guiblock.block;

import github.fafaovo.common.Itemgroup.ExampleGroup;
import github.fafaovo.common.guiblock.tileentity.ExampleInitialEntity;
import net.minecraft.block.Block;
import net.minecraft.block.BlockState;
import net.minecraft.block.material.Material;
import net.minecraft.item.BlockItem;
import net.minecraft.item.BlockItemUseContext;
import net.minecraft.item.Item;
import net.minecraft.state.DirectionProperty;
import net.minecraft.state.StateContainer;
import net.minecraft.state.properties.BlockStateProperties;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.Direction;
import net.minecraft.util.Mirror;
import net.minecraft.util.Rotation;
import net.minecraft.world.IBlockReader;
import net.minecraftforge.event.RegistryEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.registries.ObjectHolder;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;

@Mod.EventBusSubscriber(bus = Mod.EventBusSubscriber.Bus.MOD)
public class ExampleInitialMachine extends Block {
    //todo
    public static final String NAME = "examplemod:initial_machine";

    //方向
    private static final DirectionProperty FACING = BlockStateProperties.HORIZONTAL_FACING;

    @ObjectHolder(NAME)
    public static ExampleInitialMachine BLOCK;

    //注册方块
    @SubscribeEvent
    public static void onRegisterBlock(@Nonnull RegistryEvent.Register<Block> event)
    {
        //todo
        event.getRegistry().register(new ExampleInitialMachine().setRegistryName(NAME));
    }

    //注册物品
    @SubscribeEvent
    public static void onRegisterItem(@Nonnull RegistryEvent.Register<Item> event)
    {
        event.getRegistry().register(new BlockItem(BLOCK, new Item.Properties().group(ExampleGroup.exampleGroup)).setRegistryName(NAME));
    }


    public ExampleInitialMachine() {
        super(Properties.create(Material.IRON).hardnessAndResistance(4));
        //初始化方向
        this.setDefaultState(this.getStateContainer().getBaseState().with(FACING, Direction.NORTH));
    }

    //该机器存在实体
    @Override
    public boolean hasTileEntity(@Nonnull BlockState state)
    {
        return true;
    }

    //创建实体
    @Override
    public TileEntity createTileEntity(@Nonnull BlockState state, @Nonnull IBlockReader world)
    {
        //todo
        return ExampleInitialEntity.TILE_ENTITY_TYPE.create();
    }





    //放置方块的朝向 玩家视角的反方向
    @Nullable
    @Override
    public BlockState getStateForPlacement(BlockItemUseContext context) {
        return this.getDefaultState().with(FACING, context.getPlacementHorizontalFacing().getOpposite());
    }

    //翻转和镜像
    @Override
    public BlockState rotate(BlockState state, Rotation rot) {
        return state.with(FACING, rot.rotate(state.get(FACING)));
    }

    @Override
    public BlockState mirror(BlockState state, Mirror mirrorIn) {
        return state.rotate(mirrorIn.toRotation(state.get(FACING)));
    }

    @Override
    protected void fillStateContainer(StateContainer.Builder<Block, BlockState> builder) {
        builder.add(FACING);
    }
}
